package org.example.elastic.lite.controller;

import org.example.elastic.lite.handler.DynamicElasticJobHandler;
import org.example.elastic.lite.jobs.ExampleDataflowJob;
import org.example.elastic.lite.jobs.ExampleDynamicSimpleJob;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping(value = "/dynamicJob")
public class DynamicElastcJobOperateController {

    @Resource
    private DynamicElasticJobHandler dynamicElasticJobHandler;

    @RequestMapping(value = "/addSimpleJob", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public String addSimpleJob(@RequestBody Map<String, Object> params) {
        String jobName = params.get("jobName").toString();
        String cron = params.get("cron").toString();
        int shardingTotalCount = Integer.parseInt(params.get("shardingTotalCount").toString());
        String parameters = params.get("parameters").toString();
        String descrption = params.get("descrption").toString();
        dynamicElasticJobHandler.addSimpleJob(jobName, cron, shardingTotalCount,new ExampleDynamicSimpleJob(), parameters, descrption);
        return "success";
    }

    @RequestMapping(value = "/addDataflowJob", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public String addDataflowJob(@RequestBody Map<String, Object> params) {
        String jobName = params.get("jobName").toString();
        String cron = params.get("cron").toString();
        int shardingTotalCount = Integer.parseInt(params.get("shardingTotalCount").toString());
        String parameters = params.get("parameters").toString();
        String descrption = params.get("descrption").toString();
        dynamicElasticJobHandler.addDataflowJob(jobName, cron, shardingTotalCount,new ExampleDataflowJob(), parameters, descrption);
        return "success";
    }

    @RequestMapping(value = "/addOneOffSimpleJob", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public String addOneOffSimpleJob(@RequestBody Map<String, Object> params) {
        String jobName = params.get("jobName").toString();
        String cron = params.get("cron").toString();
        int shardingTotalCount = Integer.parseInt(params.get("shardingTotalCount").toString());
        String parameters = params.get("parameters").toString();
        String descrption = params.get("descrption").toString();
        dynamicElasticJobHandler.addOneOffSimpleJob(jobName, cron, shardingTotalCount,new ExampleDynamicSimpleJob(), parameters, descrption);
        return "success";
    }



    @RequestMapping(value = "/updateJob", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateJob(@RequestBody Map<String, Object> params) {
        String jobName = params.get("jobName").toString();
        String cron = params.get("cron").toString();
        dynamicElasticJobHandler.updateJob(jobName, cron);
        return "success";
    }

    @RequestMapping(value = "/removeJob", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public String removeJob(@RequestBody Map<String, Object> params) {
        String jobName = params.get("jobName").toString();
        dynamicElasticJobHandler.removeJob(jobName);
        return "success";
    }
}
